package com.linzp.bishe.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.linzp.bishe.common.Constants;
import com.linzp.bishe.common.Result;
import com.linzp.bishe.controller.exception.ServiceException;
import com.linzp.bishe.entity.Accounts;
import com.linzp.bishe.entity.Meeting;
import com.linzp.bishe.excel.MeetingExcel;
import com.linzp.bishe.excel.MeetingExcelListener;
import com.linzp.bishe.model.StatusModel;
import com.linzp.bishe.service.AccountsService;
import com.linzp.bishe.service.MeetingService;
import com.linzp.bishe.service.SaveExcelData;
import com.linzp.bishe.service.UsersService;
import com.linzp.bishe.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 林泽鹏
 * @since 2021-11-01
 */
@RestController
@RequestMapping("/meeting")
@Transactional
public class MeetingController {

	@Autowired
	private MeetingService meetingService;

	@Autowired
	private AccountsService accountsService;

	@Autowired
	private UsersService usersService;

	@Autowired
	private SaveExcelData saveExcelData;
	/**
	 * @Description 增加消息的时候用一个缓存先存放着防止网络中断或者不想要产生垃圾
	 * @Param [file]
	 * @return com.linzp.bishe.common.Result
	 */
	@PostMapping("/fileCache")
	public Result addSuportingMaterialsCache(@RequestParam("file") MultipartFile file){
		Result success = Result.success();
		success.put("saveName",meetingService.saveMultipartFile(file));
		return success;
	}

	/**
	 * @Description 更新文件
	 * @Param [file]
	 * @return com.linzp.bishe.common.Result
	 */
	@PostMapping("/upFile")
	public Result upSuportingMaterials(@RequestParam("file") MultipartFile file,String fileName,Integer id){
		Result success = Result.success();
		String multipartFile;
		boolean cheak = true;
		if(fileName == null || fileName.isEmpty()){
			multipartFile = meetingService.saveMultipartFile(file);
			meetingService.saveCacheToFile(multipartFile);
		}
		else{
			multipartFile = meetingService.updateMultipartFile(file, fileName);
			if (multipartFile.equals(fileName)){
				cheak = false;
			}
		}
		if (cheak){
			UpdateWrapper<Meeting> update = new UpdateWrapper<>();
			update.eq("id",id);
			update.set("supporting_material",multipartFile);
			meetingService.update(update);
		}
		return success;
	}

	/**
	 * @Description 添加meeting信息
	 * @Param [meeting]
	 * @return com.linzp.bishe.common.Result
	 */
	@PostMapping("")
	public Result addMeeting(@RequestBody Meeting meeting){
		Result success = Result.success();
		if (meetingService.cheakRepeat(meeting.getMeetingName(),meeting.getUserId()))
			throw new ServiceException("您已将会议导入",null);

		meeting.setStatus(Constants.NOTSUBMIT);
		meetingService.save(meeting);
		if(meeting.getSupportingMaterial() !=null & !meeting.getSupportingMaterial().isEmpty()){
			meetingService.saveCacheToFile(meeting.getSupportingMaterial());
		}
		return success;
	}

	/**
	 * @Description 普通用户通过ID获得数据
	 * @Param [httpServletRequest, current, page, searchMeeting, searchStatus]
	 * @return com.linzp.bishe.common.Result
	 */
	@GetMapping("")
	public  Result getMeetings(HttpServletRequest httpServletRequest, Integer current, Integer page,
	                           String searchMeeting, String searchStatus){
		Accounts account = accountsService.checkAccountStatus(httpServletRequest);
		Result success = Result.success();
		Integer id = usersService.getByAccountIDUserID(account.getId()).getId();
		meetingService.getByIdAllMeeting(success,id,current,page,searchMeeting,searchStatus);
		return success;
	}

	/**
	 * @Description 管理员获得数据
	 * @Param [httpServletRequest, current, page, searchMeeting, searchStatus]
	 * @return com.linzp.bishe.common.Result
	 */
	@GetMapping("/admin")
	public  Result getAdminMeetings(HttpServletRequest httpServletRequest, Integer current, Integer page,
	                           String searchMeeting, String searchStatus){
		accountsService.checkAccountIsAdmin(httpServletRequest);
		Result success = Result.success();
		meetingService.getByIdAllMeeting(success,null,current,page,searchMeeting,searchStatus);
		return success;
	}

	/**
	 * @Description 修改meeting信息
	 * @Param [meeting]
	 * @return com.linzp.bishe.common.Result
	 */
	@PutMapping("")
	public Result updateMeeting(@RequestBody Meeting meeting){
		Result success = Result.success();
		meetingService.updateById(meeting);
		return success;
	}


	/**
	 * @Description 通过ID删除信息
	 * @Param [meeting]
	 * @return com.linzp.bishe.common.Result
	 */
	@DeleteMapping("")
	public Result deleteMeetings(@RequestBody Meeting meeting){
		Result success = Result.success();
		meetingService.deleteMeetingById(meeting.getId(),meeting.getSupportingMaterial());
		return success;
	}

	@PutMapping("/status")
	public Result dealStatus(@RequestBody StatusModel statusModel){
		meetingService.dealStatus(statusModel.getId(),statusModel.getStatus());
		return Result.success();
	}

	@PostMapping("/addExcel")
	public Result addExcel(HttpServletRequest httpServletRequest,@RequestParam("file") MultipartFile file){
		if(file.isEmpty()){
			throw new ServiceException("没有文件",null);
		}
		if(!Constants.EXCEL.equals(file.getContentType())){
			throw new ServiceException("注意文件后缀为.xlsx",null);
		}
		Result success = Result.success();
		Accounts account = accountsService.checkAccountStatus(httpServletRequest);
		ThreadLocalUtils.localVar.set(account.getId().toString());
		try {
			EasyExcel.read(file.getInputStream(), MeetingExcel.class,new MeetingExcelListener(saveExcelData)).sheet().doRead();
		} catch (IOException e) {
			throw new ServiceException("注意导入指定模板文件",null);
		}

		return success;
	}
}
